//RTK
import {
  createAsyncThunk,
  createSlice,
  type PayloadAction,
} from "@reduxjs/toolkit";
// 自己的
import { coffeeSelectionType } from "@/type/coffeeType";

const initialState: coffeeSelectionType = {
  showCoffeeSelection: false,
  count: 1,
  size: "middle",
  iceLevel: "ice",
  sugerLevel: "normalSuger",
};

export const fetchCoffeeSelectionAction = createAsyncThunk(
  "fetchCoffeeSelection",
  (param, { dispatch }) => {
    dispatch(changeShowCoffeeSelection(true));
  }
);

const coffeeSelectionSlice = createSlice({
  name: "coffeeSelection",
  initialState,
  reducers: {
    changeShowCoffeeSelection(state, { payload }: PayloadAction<boolean>) {
      state.showCoffeeSelection = payload;
    },
    changeCoffeeCountSelection(state, { payload }: PayloadAction<number>) {
      // 若传过来为0，即重置count
      if (payload === 0) {
        state.count = 1;
      } else {
        state.count += payload;
      }
    },
    changeSizeSelection(state, { payload }: PayloadAction<"big" | "middle">) {
      state.size = payload;
    },
    changeIceSelection(
      state,
      { payload }: PayloadAction<"ice" | "lessIce" | "noIce">
    ) {
      state.iceLevel = payload;
    },
    changeSugerSelection(
      state,
      {
        payload,
      }: PayloadAction<"normalSuger" | "lessSuger" | "halfSuger" | "noSuger">
    ) {
      state.sugerLevel = payload;
    },
  },
});

export const {
  changeShowCoffeeSelection,
  changeSizeSelection,
  changeIceSelection,
  changeSugerSelection,
  changeCoffeeCountSelection,
} = coffeeSelectionSlice.actions;
export default coffeeSelectionSlice.reducer;
